import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path:"/",
        name:"Commodity",
        component:()=>import("../views/Commodity.vue")
      },
      {
        path:"/trading",
        name:"Trading",
        component:()=>import("../views/Trading.vue")
      },
      {
        path:"/user",
        name:"User",
        component:()=>import("../views/User.vue")
      },
      {
        path:"/payment",
        name:"Payment",
        component:()=>import("../views/Payment.vue")
      },
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:"/login",//登录
    name:"Login",
    component:()=>import("../views/Login.vue")
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  var token = localStorage.getItem("token");
  if(token){
    next();
  }else{
    if(to.name=="Login"){
      next();
    }else{
      next("/login");
    }
  }
  
})

export default router
